
package com.example.classconnect;

import java.net.MalformedURLException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.microsoft.windowsazure.mobileservices.MobileServiceClient;
import com.microsoft.windowsazure.mobileservices.MobileServiceTable;
import com.microsoft.windowsazure.mobileservices.ServiceFilterResponse;
import com.microsoft.windowsazure.mobileservices.TableOperationCallback;

public class RegistrationActivity extends Activity {
	
	private MobileServiceClient mClient;
	private MobileServiceTable<User> mUserTable;
	private ProgressBar mProgressBar;
	
	private EditText mEmailView;
	private EditText mPasswordView;
	private EditText mPassword2View;
	private EditText mfNameView;
	private EditText mlNameView;
	private EditText mSchoolNumView;
	private User newUser = new User();
	
	public static final String EXTRA_EMAIL = "com.example.android.authenticatordemo.extra.EMAIL";


	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_registration);
		//mProgressBar = (ProgressBar) findViewById(R.id.ProgressBar);

		// Initialize the progress bar
		//mProgressBar.setVisibility(ProgressBar.GONE);
		
		mfNameView = (EditText) findViewById(R.id.enterClass);
		mlNameView = (EditText) findViewById(R.id.editText2);
		mEmailView = (EditText) findViewById(R.id.editText3);
		mPasswordView = (EditText) findViewById(R.id.editText4);
		mPassword2View = (EditText) findViewById(R.id.editText5);
		//mSchoolNumView = (EditText) findViewById(R.id.editText6);
		
		//registerForContextMenu(mSchoolNumView);
		
			// TODO: 2 Create the Azure client instance, with URL and key
			try {
				mClient = new MobileServiceClient(
					      "https://classconnect.azure-mobile.net/",
					      "HzaUWkJBnWTXxCuREwxyQSaxJSyyJm51",
					      this
					);
				// TODO: 3 Get the Mobile Service Table instance to use
				mUserTable = mClient.getTable(User.class);
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
			//@Override
			//public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		      //  super.onCreateContextMenu(menu, v, menuInfo);
		        
		     // }
			//@Override
			/*public boolean onContextItemSelected(MenuItem item) {
		        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

		        switch(item.getItemId()) {
		        case R.id.item1:
		        	mSchoolNumView.setHint("Towson");
		        	newUser.setSchool(1);
		            return true;
		        case R.id.item2:
		        	mSchoolNumView.setHint("UMD");
		        	newUser.setSchool(2);
		            return true;
		            
		        case R.id.item3:
		        	mSchoolNumView.setHint("UBalt");
		        	newUser.setSchool(3);
		        	return true;
		        default:
		            return super.onContextItemSelected(item);
		        }
		    }*/
			
			public void verify(View view){
				if (mClient == null) {
					return;
				}
				String fName = mfNameView.getText().toString();
				String email = mEmailView.getText().toString();
				String pwd1 = mPasswordView.getText().toString();
				String pwd2 = mPassword2View.getText().toString();
				if (fName.matches("")
					|| email.matches("")
					|| pwd1.matches("")
					|| pwd2.matches("")){
					//|| mSchoolNumView.getText().toString() == null){
					Toast.makeText( this,
							"Please fill in required information!",
							Toast.LENGTH_LONG).show();
					return;
				}
				if(!pwd1.matches(pwd2)){
					Toast.makeText( this,
							"Error: Passwords do not match!",
							Toast.LENGTH_LONG).show();
					return;
				} if (!mEmailView.getText().toString().contains("@")) {
					Toast.makeText( this,
							"Please enter valid email!",
							Toast.LENGTH_LONG).show();
					return;
				}
				addUser(view);
			}
			
			public void addUser(View view){
				newUser.setEmail(mEmailView.getText().toString());
				newUser.setfName(mfNameView.getText().toString());
				newUser.setlName(mlNameView.getText().toString());
				newUser.setPassword(mPasswordView.getText().toString());
				mUserTable.insert(newUser, new TableOperationCallback<User>() {

					@Override
					public void onCompleted(User entity, Exception exception,
							ServiceFilterResponse response) {
						// TODO Auto-generated method stub
						if (exception == null) {
							System.out.println("Horray");
							Toast.makeText(getApplicationContext(), "Account created!", Toast.LENGTH_SHORT).show();
							Intent myIntent = new Intent(RegistrationActivity.this, LoginActivity.class);
							myIntent.putExtra("Email", mEmailView.getText().toString());
							// *** in the future we want to pass user credentials to next activity
							startActivity(myIntent);
							finish();
						} else {
							System.out.println(exception.toString());
						}
					}
				});
			}

	}


